package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcStockTakeService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 盘点
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/stock_take")
@RestController
public class XqcStockTakeController {

    @Autowired
    IXqcStockTakeService stockTakeService;

    /**
     * 保存盘点方案
     */
    @PostMapping("/save_stock_take")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billName", label = "方案名称", notNull = true),
    } )
    public Object saveStockTake(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.saveStockTake(_userId, name, param);
    }

    /**
     * 审核盘点方案
     */
    @PostMapping("/audit_stock_take")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object auditStockTake(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.auditStockTake(_userId, name, param);
        return null;
    }

    /**
     * 反审盘点方案
     */
    @PostMapping("/unaudit_stock_take")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object unauditStockTake(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.unauditStockTake(_userId, name, param);
        return null;
    }

    /**
     * 获取盘点方案详细(恢复)
     */
    @PostMapping("/get_stock_take_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object getStockTakeDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return stockTakeService.getStockTakeDetail(_userId, billNo);
    }

    /**
     * 删除盘点方案
     */
    @PostMapping("/delete_stock_take")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object deleteStockTake(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.deleteStockTake(_userId, name, param);
        return null;
    }

    /**
     * 查询盘点列表
     */
    @PostMapping("/get_stock_take_list")
    @VtuzxApi
    public Object getStockTakeList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getStockTakeList(_userId, name, param);
    }

    /**
     * 下发到盘点作业
     */
    @PostMapping("/to_stock_take_task")
    @VtuzxApi
    public Object toStockTakeTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.toStockTakeTask(_userId, name, param);
        return null;
    }

    /**
     * 审核盘点作业
     */
    @PostMapping("/audit_stock_take_task")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object auditStockTakeTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.auditStockTakeTask(_userId, name, param);
        return null;
    }

    /**
     * 反审盘点作业
     */
    @PostMapping("/unaudit_stock_take_task")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object unauditStockTakeTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.unauditStockTakeTask(_userId, name, param);
        return null;
    }

    /**
     * 查询盘点作业列表
     */
    @PostMapping("/get_stock_take_task_list")
    @VtuzxApi
    public Object getStockTakeTaskList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        return stockTakeService.getStockTakeTaskList(_userId, param);

    }

    /**
     * 查询盘点作业详细
     */
    @PostMapping("/get_stock_take_task_detail")
    @VtuzxApi
    public Object getStockTakeTaskDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getStockTakeTaskDetail(_userId, name, param);

    }

    /**
     * 导出盘点作业详细
     */
    @PostMapping("/download_stock_take_task_detail")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadStockTakeTaskDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return stockTakeService.downloadStockTakeTaskDetail(token, param);
    }

    /**
     * 查询盘点作业详细箱号
     */
    @PostMapping("/get_stock_take_task_detail_box")
    @VtuzxApi
    public Object getStockTakeTaskDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getStockTakeTaskDetailBox(_userId, name, param);

    }


    /**
     * 查询用户盘点作业
     */
    @PostMapping("/get_user_task")
    @VtuzxApi
    public Object getUserTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getUserTask(_userId, name, param);

    }
    /**
     * 查询用户盘点作业详细
     */
    @PostMapping("/get_user_task_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "作业编码", notNull = true),
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getUserTaskDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        return stockTakeService.getUserTaskDetail(_userId, name, billNo, stockNo, locationNo);

    }
    /**
     * 查询用户盘点作业详细详箱子
     */
    @PostMapping("/get_user_task_detail_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "key", label = "key", notNull = true),
            @VtuzxValidator(key = "billNo", label = "作业编码", notNull = true)
    } )
    public Object getUserTaskDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String key = VtuzxUtil.getString(param, "key");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return stockTakeService.getUserTaskDetailBox(_userId, name, key, billNo);
    }

    /**
     * 查询用户盘点作业详细详箱子
     */
    @PostMapping("/add_user_task_detail_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "count", label = "重量", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位编码", notNull = true),
            @VtuzxValidator(key = "billNo", label = "作业编号", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
    } )
    public Object addUserTaskDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        double count = VtuzxUtil.getDouble(param, "count");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        stockTakeService.addUserTaskDetailBox(_userId, name, boxNo, locationNo, billNo, count);
        return null;
    }

    /**
     * 查询用户盘点作业详细详箱子
     */
    @PostMapping("/add_user_task_detail_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "locationNo", label = "作业编码", notNull = true),
            @VtuzxValidator(key = "billNo", label = "作业编号", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号", notNull = true),
    } )
    public Object addUserTaskDetailBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        String billNo = VtuzxUtil.getString(param, "billNo");
        stockTakeService.addUserTaskDetailBoxList(_userId, name, boxList, locationNo, billNo);
        return null;
    }

    /**
     * 删除箱子
     */
    @PostMapping("/delete_user_detail_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "billNo", label = "作业编码", notNull = true)
    } )
    public Object deleteUserDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        stockTakeService.deleteUserDetailBox(_userId, name, boxNo, billNo);
        return null;
    }

    /**
     * 删除物料
     */
    @PostMapping("/delete_user_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "key", label = "key", notNull = true),
            @VtuzxValidator(key = "billNo", label = "作业编码", notNull = true)
    } )
    public Object deleteUserDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String key = VtuzxUtil.getString(param, "key");
        String billNo = VtuzxUtil.getString(param, "billNo");
        stockTakeService.deleteUserDetail(_userId, name, key, billNo);
        return null;
    }
    /**
     * 完成盘点作业
     */
    @PostMapping("/save_user_task")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "作业编码", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
    } )
    public Object saveUserTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        stockTakeService.saveUserTask(_userId, name, billNo, stockNo, locationNo);
        return null;
    }

    /**
     * 根据RFID从即时库存单中获取物料批量入库
     */
    @PostMapping("/get_material_by_rfid_in_stock")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单据号", notNull = true),
            @VtuzxValidator(key = "rfid", label = "RFID", notNull = true),
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getMaterialByRfidInStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String billNo = VtuzxUtil.getObject(param, "billNo");
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid");
        String stockNo = VtuzxUtil.getObject(param, "stockNo");
        String locationNo = VtuzxUtil.getObject(param, "locationNo");
        return stockTakeService.getMaterialByRfidInStock(billNo, rfidList, stockNo, locationNo);
    }

    /**
     * 根据箱号从仓库中获取物料
     */
    @PostMapping("/get_material_by_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单据号", notNull = true),
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true)
    } )
    public Object getMaterialByBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String billNo = VtuzxUtil.getObject(param, "billNo");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getObject(param, "locationNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
//        String boxNo;
//        if (boxNoKey.contains("|;")) {
//            boxNo = boxNoKey.substring(boxNoKey.lastIndexOf("|;") + 2);
//        } else {
//            boxNo = boxNoKey;
//        }
        return stockTakeService.getMaterialBox(billNo, stockNo, locationNo, boxNo);
    }

    /**
     * 生成盘盈亏单
     */
    @PostMapping("/to_TPAL_order")
    @VtuzxApi
    public Object toTPALOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        stockTakeService.toTPALOrder(_userId, name, billNo);
        return null;
    }


    /**
     * 审核盘盈亏单
     */
    @PostMapping("/audit_profit_and_loss")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object auditProfitAndLoss(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.auditProfitAndLoss(_userId, name, param);
        return null;
    }

    /**
     * 反审盘盈亏单
     */
    @PostMapping("/unaudit_profit_and_loss")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "方案编码", notNull = true)
    } )
    public Object unauditProfitAndLoss(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        stockTakeService.unauditProfitAndLoss(_userId, name, param);
        return null;
    }

    /**
     * 查询盘盈亏列表
     */
    @PostMapping("/get_profit_and_loss_list")
    @VtuzxApi
    public Object getProfitAndLossList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        return stockTakeService.getProfitAndLossList(_userId, param);

    }

    /**
     * 查询盘盈亏详细
     */
    @PostMapping("/get_profit_and_loss_detail")
    @VtuzxApi
    public Object getProfitAndLossDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getProfitAndLossDetail(_userId, name, param);

    }

    /**
     * 查询盘盈亏详细箱号
     */
    @PostMapping("/get_profit_and_loss_detail_box")
    @VtuzxApi
    public Object getProfitAndLossDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getProfitAndLossDetailBox(_userId, name, param);

    }

    /**
     * 查询仓位列表
     */
    @PostMapping("/get_location_list")
    @VtuzxApi
    public Object getLocationList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return stockTakeService.getLocationList(_userId, name, stockNo);

    }

    /**
     * 查询有效rfid数量
     */
    @PostMapping("/get_rfid_count")
    @VtuzxApi
    public Object getRfidCount(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<String> rfidList = VtuzxUtil.getObject(param, "rfidList");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return stockTakeService.getRfidCount(_userId, name, rfidList, billNo, locationNo);

    }
    /**
     * 查询有效boxNo数量
     */
    @PostMapping("/get_boxNo_count")
    @VtuzxApi
    public Object getBoxNoCount(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<String> boxNoList = VtuzxUtil.getObject(param, "boxNoList");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return stockTakeService.getBoxNoCount(_userId, name, boxNoList, billNo, locationNo);

    }


    /**
     * 查询总箱数和已扫数
     */
    @PostMapping("/get_box_count")
    @VtuzxApi
    public Object getBoxCount(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return stockTakeService.getBoxCount(_userId, name, param);

    }

    /**
     * 下载盘盈亏单列表
     */
    @PostMapping("/download_profit_and_loss")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object downloadProfitAndLoss(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String id = VtuzxUtil.getString(param, "id");
        return stockTakeService.downloadProfitAndLoss(_userId, name, id, billNo);
    }

}
